package com.app.service.Impl;

import com.app.common.CalculateDistance;
import com.app.mapper.VillageFairMapper;
import com.app.model.CategoryModel;
import com.app.model.TownshipModel;
import com.app.model.VillageFairModel;
import com.app.service.CategoryService;
import com.app.service.TownService;
import com.app.service.VillageFairService;
import com.app.vo.FairVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

@Service
public class VillageFairServiceImpl implements VillageFairService {
    @Autowired
    private VillageFairMapper villageFairMapper;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private TownService townService;

    @Override
    public void update(VillageFairModel model) {
        villageFairMapper.updateById(model);
    }

    @Override
    public void insert(VillageFairModel model) {
        villageFairMapper.insert(model);
    }

    @Override
    public void deleteById(Integer id) {
        villageFairMapper.deleteById(id);
    }

    @Override
    public FairVo selectList(Integer page, Integer limit, String keyword, Integer type, Integer status, Integer township) {
        FairVo vo = new FairVo();
        page = (page - 1) * limit;
        List<VillageFairModel> villageFairModelList = villageFairMapper.getFairList(page, limit, keyword, type, status, township);
        for (int i = 0; i < villageFairModelList.size(); i++) {
            if (villageFairModelList.get(i).getType() != null) {
                CategoryModel categoryById = categoryService.getCategoryById(villageFairModelList.get(i).getType());
                if (categoryById != null) {
                    villageFairModelList.get(i).setTypeText(categoryById.getName());
                }
            }
            if (villageFairModelList.get(i).getSwiperImage() != null && !villageFairModelList.get(i).getSwiperImage().equals("")) {
                String[] split = villageFairModelList.get(i).getSwiperImage().split(",");
                villageFairModelList.get(i).setSwiperImages(Arrays.asList(split));
            }
            if (villageFairModelList.get(i).getIntroduction() != null && !villageFairModelList.get(i).getIntroduction().equals("")) {
                String[] split1 = villageFairModelList.get(i).getIntroduction().split(",");
                villageFairModelList.get(i).setIntroductions(Arrays.asList(split1));
            }
            if (villageFairModelList.get(i).getStatus() != null && villageFairModelList.get(i).getStatus() == 0) {
                villageFairModelList.get(i).setStatusText("待审核");
            }
            if (villageFairModelList.get(i).getStatus() != null && villageFairModelList.get(i).getStatus() == 1) {
                villageFairModelList.get(i).setStatusText("通过");
            }
            if (villageFairModelList.get(i).getStatus() != null && villageFairModelList.get(i).getStatus() == 2) {
                villageFairModelList.get(i).setStatusText("未通过");
            }
        }
        List<TownshipModel> townList = townService.getTownList();
        for (int i = 0; i < villageFairModelList.size(); i++) {
            if (townList != null) {
                for (int i1 = 0; i1 < townList.size(); i1++) {
                    if (villageFairModelList.get(i).getTownship() != null && villageFairModelList.get(i).getTownship().equals(townList.get(i1).getId())) {
                        villageFairModelList.get(i).setTownshipText(townList.get(i1).getTownshipName());
                    }
                }
            }
        }
        int count = villageFairMapper.getFairCount(keyword, type, status);
        vo.setList(villageFairModelList);
        vo.setCount(count);
        return vo;
    }

    @Override
    public FairVo selectList(Integer page, Integer limit, String keyword, Integer type, Double longitude, Double latitude, Integer township) {
        FairVo vo = new FairVo();
        page = (page - 1) * limit;
        List<VillageFairModel> villageFairModelList = villageFairMapper.selectFairList(page, limit, keyword, type, township);
        for (int i = 0; i < villageFairModelList.size(); i++) {
            if (villageFairModelList.get(i).getType() != null) {
                CategoryModel categoryById = categoryService.getCategoryById(villageFairModelList.get(i).getType());
                if (categoryById != null) {
                    villageFairModelList.get(i).setTypeText(categoryById.getName());
                }
            }
            if (villageFairModelList.get(i).getSwiperImage() != null && !villageFairModelList.get(i).getSwiperImage().equals("")) {
                String[] split = villageFairModelList.get(i).getSwiperImage().split(",");
                villageFairModelList.get(i).setSwiperImages(Arrays.asList(split));
            }
            if (villageFairModelList.get(i).getLongitude() != null && villageFairModelList.get(i).getLatitude() != null) {
                double longitude1 = Double.parseDouble(villageFairModelList.get(i).getLongitude());
                double latitude1 = Double.parseDouble(villageFairModelList.get(i).getLatitude());

                if (latitude != null && longitude != null) {
                    Double haversine = CalculateDistance.haversine(latitude1, longitude1, latitude, longitude);
                    if (haversine != 0) {
                        DecimalFormat df = new DecimalFormat("0.0");
                        String formatted = df.format(haversine);
                        villageFairModelList.get(i).setDistance(formatted + "km");
                    }
                } else {
                    villageFairModelList.get(i).setDistance("暂无");
                }
            } else {
                villageFairModelList.get(i).setDistance("暂无");
            }
        }
        List<TownshipModel> townList = townService.getTownList();
        for (int i = 0; i < villageFairModelList.size(); i++) {
            if (townList != null) {
                for (int i1 = 0; i1 < townList.size(); i1++) {
                    if (villageFairModelList.get(i).getTownship() != null && villageFairModelList.get(i).getTownship().equals(townList.get(i1).getId())) {
                        villageFairModelList.get(i).setTownshipText(townList.get(i1).getTownshipName());
                    }
                }
            }
        }
        int count = villageFairMapper.selectFairCount(keyword, type);
        vo.setList(villageFairModelList);
        vo.setCount(count);
        return vo;
    }

    @Override
    public VillageFairModel selectById(Integer id) {
        VillageFairModel villageFairModel = villageFairMapper.selectById(id);
        if (villageFairModel.getType() != null) {
            CategoryModel categoryById = categoryService.getCategoryById(villageFairModel.getType());
            if (categoryById != null) {
                villageFairModel.setTypeText(categoryById.getName());
            }
        }
        if (villageFairModel.getIntroduction() != null && !villageFairModel.getIntroduction().equals("")) {
            String[] split1 = villageFairModel.getIntroduction().split(",");
            villageFairModel.setIntroductions(Arrays.asList(split1));
        }
        if (villageFairModel.getSwiperImage() != null && !villageFairModel.getSwiperImage().equals("")) {
            String[] split = villageFairModel.getSwiperImage().split(",");
            villageFairModel.setSwiperImages(Arrays.asList(split));
        }
        return villageFairModel;
    }

    @Override
    public List<VillageFairModel> selectByUserId(Integer id) {
        QueryWrapper<VillageFairModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        return villageFairMapper.selectList(queryWrapper);
    }
}
